package bp.en;

/**
 * 分页查询结果类
 * 包含分页数据和分页信息
 */
public class PageResult {
    
    /**
     * 当前页的数据集合
     */
    private Entities data;
    
    /**
     * 当前页码（从1开始）
     */
    private int pageIndex;
    
    /**
     * 每页记录数
     */
    private int pageSize;
    
    /**
     * 总记录数
     */
    private int totalCount;
    
    /**
     * 总页数
     */
    private int totalPages;
    
    /**
     * 是否有上一页
     */
    private boolean hasPrevious;
    
    /**
     * 是否有下一页
     */
    private boolean hasNext;
    
    /**
     * 当前页实际记录数
     */
    private int currentPageCount;

    public PageResult() {
    }

    public PageResult(Entities data, int pageIndex, int pageSize, int totalCount) {
        this.data = data;
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.currentPageCount = data != null ? data.size() : 0;
        
        // 计算总页数
        this.totalPages = (int) Math.ceil((double) totalCount / pageSize);
        
        // 计算是否有上一页/下一页
        this.hasPrevious = pageIndex > 1;
        this.hasNext = pageIndex < totalPages;
    }

    // Getter 和 Setter 方法
    public Entities getData() {
        return data;
    }

    public void setData(Entities data) {
        this.data = data;
        this.currentPageCount = data != null ? data.size() : 0;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
        // 重新计算是否有上一页/下一页
        this.hasPrevious = pageIndex > 1;
        this.hasNext = pageIndex < totalPages;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
        // 重新计算总页数
        this.totalPages = (int) Math.ceil((double) totalCount / pageSize);
        this.hasNext = pageIndex < totalPages;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
        // 重新计算总页数
        this.totalPages = (int) Math.ceil((double) totalCount / pageSize);
        this.hasNext = pageIndex < totalPages;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public boolean isHasPrevious() {
        return hasPrevious;
    }

    public boolean isHasNext() {
        return hasNext;
    }

    public int getCurrentPageCount() {
        return currentPageCount;
    }

    /**
     * 获取起始记录序号（用于显示"显示第X到第Y条记录"）
     */
    public int getStartIndex() {
        if (totalCount == 0) {
            return 0;
        }
        return (pageIndex - 1) * pageSize + 1;
    }

    /**
     * 获取结束记录序号
     */
    public int getEndIndex() {
        if (totalCount == 0) {
            return 0;
        }
        return Math.min(getStartIndex() + currentPageCount - 1, totalCount);
    }

    /**
     * 是否为空结果
     */
    public boolean isEmpty() {
        return currentPageCount == 0;
    }

    /**
     * 是否为第一页
     */
    public boolean isFirstPage() {
        return pageIndex == 1;
    }

    /**
     * 是否为最后一页
     */
    public boolean isLastPage() {
        return pageIndex == totalPages || totalPages == 0;
    }

    @Override
    public String toString() {
        return String.format("PageResult{pageIndex=%d, pageSize=%d, totalCount=%d, totalPages=%d, currentPageCount=%d}", 
                pageIndex, pageSize, totalCount, totalPages, currentPageCount);
    }
}